package com.andreas.serviceImp;

import com.andreas.Utils.Md5Utils;
import com.andreas.exception.BussinessException;
import com.andreas.mapper.EvaluationMapper;
import com.andreas.mapper.MemberMapper;
import com.andreas.mapper.MemberReadStateMapper;
import com.andreas.pojo.Evaluation;
import com.andreas.pojo.Member;
import com.andreas.pojo.MemberReadState;
import com.andreas.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author andreaszhou
 * @ClassName MemberServiceImpl
 * @Description TODO
 * @date 2021/1/21 11:09
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private MemberReadStateMapper memberReadStateMapper;
    @Resource
    private EvaluationMapper evaluationMapper;

    @Override
    @Transactional(readOnly = false)
    public Member addMember(String username, String password, String nickname, String verifyCode) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        List<Member> members = memberMapper.selectList(queryWrapper);
        if (members.size() > 0) {
            throw new BussinessException("MO1", "用户名已经存在");
        }
        Member member = new Member();
        member.setUsername(username);
        member.setNickName(nickname);
        int salt = new Random().nextInt(1000) + 1000;
        String md5Digest = Md5Utils.md5Digest(username, salt);
        member.setPassword(md5Digest);
        member.setSalt(salt);
        member.setCreateTime(new Date());
        memberMapper.insert(member);
        return member;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    public Member checkLogin(String username, String password, String verifyCode) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Member member = memberMapper.selectOne(queryWrapper);
        if (member == null) {
            throw new BussinessException("M02", "用户名不存在");
        }
        String md5Digest = Md5Utils.md5Digest(password, member.getSalt());
        if (!(member.getPassword().equals(md5Digest))) {
            throw new BussinessException("M03", "密码输入错误");
        }
        return member;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
    public MemberReadState selectMemberReadState(int memberId, int bookId) {
        QueryWrapper<MemberReadState> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("book_id",bookId);
        MemberReadState memberReadState = memberReadStateMapper.selectOne(queryWrapper);
        return memberReadState;
    }


    /*
    * 更新用户阅读状态
    * */
    @Override
    @Transactional(readOnly = false)
    public MemberReadState updateMemberReadState(int memberId, int bookId, int readState) {
        QueryWrapper<MemberReadState> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("book_id",bookId);
        MemberReadState memberReadState = memberReadStateMapper.selectOne(queryWrapper);
        // 有则更新，无则新增
        if (memberReadState==null){
            memberReadState = new MemberReadState();
            memberReadState.setBookId(bookId);
            memberReadState.setMemberId(memberId);
            memberReadState.setReadState(readState);
            memberReadState.setCreateTime(new Date());
            memberReadStateMapper.insert(memberReadState);
        }else {
            memberReadState.setReadState(readState);
            memberReadStateMapper.updateById(memberReadState);
        }
        return memberReadState;
    }

    /*
    * 添加评论
    * */
    @Override
    @Transactional(readOnly = false)
    public Evaluation addEvaluation(int memberId, int bookId, int score, String content) {
        Evaluation evaluation = new Evaluation();
        evaluation.setMemberId(memberId);
        evaluation.setBookId(bookId);
        evaluation.setScore(score);
        evaluation.setContent(content);
        evaluation.setEnjoy(0);
        evaluation.setState("enable");
        evaluation.setCreateTime(new Date());
        evaluationMapper.insert(evaluation);
        return evaluation;
    }
    /*
    * 添加点赞
    * */

    @Override
    public Evaluation addEnjoy(int evaluationId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("evaluation_id",evaluationId);
        Evaluation evaluation = evaluationMapper.selectOne(queryWrapper);
        evaluation.setEnjoy(evaluation.getEnjoy()+1);
        evaluationMapper.updateById(evaluation);
        return evaluation;
    }
}
